package com.arges.controller;

import java.math.BigInteger;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author fanweitao
 * @date 2023/03/21
 */
public class Test9 {

    public static void main(String[] args) {
        System.out.println(toHex(new byte[]{1, 2}));
    }

    /**
     * 二进制字符串转十六进制字符串
     *
     * @param array 数组
     * @return
     */
    private static String toHex(byte[] array) {
        BigInteger bi = new BigInteger(1, array);
        String hex = bi.toString(16);
        int paddingLength = (array.length * 2) - hex.length();
        if (paddingLength > 0) {
            String format = "%0" + paddingLength + "d";
            return String.format(format, 0) + hex;
        } else {
            return hex;
        }

    }
}
